package com.xnyy.lottery.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.xnyy.lottery.common.CommonResult;
import com.xnyy.lottery.domain.dto.ParkingSpaceConfirmDTO;
import com.xnyy.lottery.domain.dto.ParkingSpaceQueryDTO;
import com.xnyy.lottery.domain.entity.ParkingSpace;
import com.xnyy.lottery.service.ParkingSpaceService;
import com.xnyy.lottery.service.RegisterService;
import com.xnyy.lottery.util.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 车位接口
 * @auther: Administrator
 * @date: 2022/9/16 23:03
 */
@AllArgsConstructor
@RestController
@RequestMapping("/parkingspace")
public class ParkingSpaceController extends BaseController {
    private final ParkingSpaceService parkingSpaceService;
    private final RegisterService registerService;

    @GetMapping("/list")
    public CommonResult list(ParkingSpaceQueryDTO dto) {
        startPage();
        List<ParkingSpace> list = parkingSpaceService.selectList(dto);
        return getDataTable(list);
    }

    @GetMapping("/lottery")
    public CommonResult lottery(@RequestParam String barCode) {
        return CommonResult.success(parkingSpaceService.lottery(barCode));
    }

    @PostMapping("/givup")
    public CommonResult lose(@RequestBody ParkingSpaceConfirmDTO dto) {
        registerService.lose(dto.getBarCode());
        return CommonResult.success();
    }

    @PostMapping("/confirm")
    public CommonResult confirm(@RequestBody ParkingSpaceConfirmDTO dto) {
        parkingSpaceService.confirm(dto);
        RegisterController bean = SpringUtil.getBean(RegisterController.class);
        bean.print(dto.getBarCode());
        return CommonResult.success();
    }

    @GetMapping("/export")
    public void export(ParkingSpaceQueryDTO dto, HttpServletResponse response) {

        List<ParkingSpace> list = parkingSpaceService.selectList(dto);
        ExcelUtil.exportExcel(list, "车位信息", "车位信息", ParkingSpace.class, "车位信息.xlsx", true, response);
    }

}
